package com.gabriel.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	private JPanel					contentPane;
	public static Stack<Processo>	processos			= new Stack<Processo>();
	JSpinner						spinner_1			= new JSpinner();
	JLabel							lblProcessadores	= new JLabel("Processadores");
	JSpinner						spinner				= new JSpinner();
	JButton							btnCriar			= new JButton("Criar");
	JButton							btnIniciar			= new JButton("Iniciar");
	JLabel							lblProcessos		= new JLabel("Processos");
	JPanel							panel				= new JPanel();
	JPanel							panel_1				= new JPanel();
	private final JScrollPane		scrollPane			= new JScrollPane();
	private final JPanel			panel_2				= new JPanel();
	private final JScrollPane		scrollPane_1		= new JScrollPane();
	private final JPanel			panel_3				= new JPanel();
	private List<Processador>		processadores		= new ArrayList<Processador>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		initComponents();
		startListeners();
	}

	private void startListeners() {
		btnCriar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				int procdrs = (Integer) spinner.getValue();
				int pcos = (Integer) spinner_1.getValue();

				panel_2.removeAll();
				panel_2.revalidate();
				panel_3.removeAll();
				panel_3.revalidate();

				processadores.clear();
				processos.clear();

				for (int i = 0; i < pcos; i++) {
					Processo processo = new Processo(i);
					panel_3.add(processo);
					panel_3.revalidate();
					processos.push(processo);
				}

				for (int i = 0; i < procdrs; i++) {
					Processador processador = new Processador(i);
					panel_2.add(processador);
					panel_2.revalidate();
					processadores.add(processador);
				}

			}
		});

		btnIniciar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Iterator<Processo> it = processos.iterator();

				while (it.hasNext()) {
					for (Processador p : processadores) {
						if (processos.size() > 0) {
							Processo proc = processos.pop();
							if (proc.getProc() == null)
								proc.setProc(p);
							p.processar(proc);
						}
					}
				}
			}
		});
	}

	private void initComponents() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 605, 408);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(41dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("99dlu"), FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC, }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(78dlu;default):grow"), FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(89dlu;default):grow"), FormFactory.RELATED_GAP_ROWSPEC, }));

		contentPane.add(lblProcessadores, "2, 2");
		contentPane.add(spinner, "4, 2");
		contentPane.add(btnCriar, "6, 2, 1, 3");
		contentPane.add(btnIniciar, "8, 2, 1, 3");
		contentPane.add(lblProcessos, "2, 4");
		contentPane.add(spinner_1, "4, 4");

		panel.setBorder(new TitledBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
												"Processadores", TitledBorder.LEADING, TitledBorder.TOP, null,
												new Color(0, 0, 0)), "", TitledBorder.LEADING, TitledBorder.TOP, null,
												null));
		contentPane.add(panel, "2, 6, 7, 1, fill, fill");
		panel.setLayout(new BorderLayout(0, 0));
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

		panel.add(scrollPane, BorderLayout.CENTER);
		panel_2.setBackground(Color.WHITE);

		scrollPane.setViewportView(panel_2);
		FlowLayout fl_panel_2 = new FlowLayout(FlowLayout.LEFT, 5, 5);
		fl_panel_2.setAlignOnBaseline(true);
		panel_2.setLayout(fl_panel_2);

		panel_1.setBorder(new TitledBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null),
												"Processos", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0,
																						0, 0)), "",
												TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_1, "2, 8, 7, 1, fill, fill");
		panel_1.setLayout(new BorderLayout(0, 0));
		scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

		panel_1.add(scrollPane_1, BorderLayout.CENTER);
		panel_3.setBackground(Color.WHITE);

		scrollPane_1.setViewportView(panel_3);
		FlowLayout fl_panel_3 = new FlowLayout(FlowLayout.LEFT, 5, 5);
		fl_panel_3.setAlignOnBaseline(true);
		panel_3.setLayout(fl_panel_3);
	}

}
